#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
"""
Description: check self build requires and install, function and uninstall
Class:
"""

import logging

from javcra.application.checkpart.check_requires import get_effected_rpms, init_env
from javcra.libs.config.global_config import FAILED


def get_all_requires(
    src_list: list,
    branch: str,
    full_list=True,
    skip_clear=False
) -> dict:
    """
    get all requires by input src rpm list

    Attribute:
        src_list: src rpm name list
        branch: branch in libs/config/checkpart/check_requires/support_branch.yaml
        full_list: True, return all list; False, only return 
        skip_clear: flag to start 

    return:
        {
            "src_rpm_name" : {
                bin: [bin_name_list],
                reason: "abi change" or "update",
                effected_by: "src_rpm_name"
            }
        }
    """
    if FAILED == get_effected_rpms.prep_get_effected_rpms():
        logging.error("failed to prepare environment to get effected rpms")
        return {}
    repofile = init_env.get_yum_repo_file(branch)
    result = get_effected_rpms.find_all_updated(src_list, branch, repofile)

    if not skip_clear:
        get_effected_rpms.clear_get_effected_rpms(skip_oscrc=False, skip_tmpdir=False)
    if not full_list:
        for k in result:
            if not result[k]["reason"]:
                result.pop(k)
    return result

